<template>
  <div class="app-container">
    <div class="block">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input
            v-model="listQuery.name"
            placeholder="请输入角色名称"
          ></el-input>
        </el-col>
        <el-col :span="6">
          <el-button
            type="success"
            icon="el-icon-search"
            @click.native="search"
            >{{ $t("button.search") }}</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-refresh"
            @click.native="reset"
            >{{ $t("button.reset") }}</el-button
          >
        </el-col>
      </el-row>
      <br />
      <el-row>
        <el-col :span="24">
          <el-button type="success" icon="el-icon-plus" @click.native="add">{{
            $t("button.add")
          }}</el-button>
          <el-button type="primary" icon="el-icon-edit" @click.native="edit">{{
            $t("button.edit")
          }}</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            @click.native="remove"
            >{{ $t("button.delete") }}</el-button
          >
          <el-button
            type="primary"
            icon="el-icon-setting"
            @click.native="openPermissions"
            >权限配置</el-button
          >
        </el-col>
      </el-row>
    </div>

    <el-table
      :data="list"
      v-loading="listLoading"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      @current-change="handleCurrentChange"
    >
      <el-table-column label="名称">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="编码">
        <template slot-scope="scope">
          {{ scope.row.tips }}
        </template>
      </el-table-column>
      <el-table-column label="所在部门">
        <template slot-scope="scope">
          {{ scope.row.deptName }}
        </template>
      </el-table-column>
      <el-table-column label="上级角色">
        <template slot-scope="scope">
          {{ scope.row.pName }}
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[10, 20, 50, 100, 500]"
      :page-size="listQuery.limit"
      :total="total"
      @size-change="changeSize"
      @current-change="fetchPage"
      @prev-click="fetchPrev"
      @next-click="fetchNext"
    >
    </el-pagination>

    <el-dialog :title="formTitle" :visible.sync="formVisible" width="70%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="编码" prop="tips">
              <el-input v-model="form.tips" minlength="1"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" minlength="1"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="上级角色">
              <el-input
                placeholder="请选择上级角色"
                v-model="form.pName"
                readonly="readonly"
                @click.native="roleTree.show = !roleTree.show"
              >
              </el-input>
              <el-tree
                v-if="roleTree.show"
                empty-text="暂无数据"
                :expand-on-click-node="false"
                :data="list"
                :props="roleTree.defaultProps"
                @node-click="handleRoleNodeClick"
                class="input-tree"
              >
              </el-tree>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序">
              <el-input v-model="form.num" type="number"></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="所在部门">
              <el-input
                placeholder="请选择所在部门"
                v-model="form.deptName"
                readonly="readonly"
                @click.native="deptTree.show = !deptTree.show"
              >
              </el-input>
              <el-tree
                v-if="deptTree.show"
                empty-text="暂无数据"
                :expand-on-click-node="false"
                :data="deptList"
                :props="deptTree.defaultProps"
                @node-click="handleDeptNodeClick"
                class="input-tree"
              >
              </el-tree>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="save">{{
            $t("button.submit")
          }}</el-button>
          <el-button @click.native="formVisible = false">{{
            $t("button.cancel")
          }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-dialog title="权限配置" :visible.sync="permissonVisible" width="25%">
      <el-form>
        <el-row>
          <el-col :span="12">
            <el-tree
              :data="permissons"
              ref="permissonTree"
              show-checkbox
              node-key="id"
              :default-checked-keys="checkedPermissionKeys"
              :props="defaultProps"
            >
            </el-tree>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button type="primary" @click="savePermissions">{{
            $t("button.submit")
          }}</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script src="./role.js"></script>
<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/common.scss";
</style>
